package mathive.client;

import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.Window;
import com.google.gwt.event.logical.shared.ResizeEvent;
import com.google.gwt.event.logical.shared.ResizeHandler;

public class WindowHandler {
	private static final float SCALE = 1.6f;
	
	private Element mWindow;
	private Element mDistance;
	
	public WindowHandler() {
		mWindow = DOM.getElementById("container");
		mDistance = DOM.getElementById("distance");
		Window.addResizeHandler(new WindowResizeHandler());
		update();
	}
	
	private void update() {
		final int width = Window.getClientWidth();
		final int height = Window.getClientHeight();
		
		int windowHeight = height;
		int windowWidth = (int)(height * SCALE);
		if (windowWidth > width) {
			windowWidth = width;
			windowHeight = (int)(windowWidth / SCALE);
		}
		
		DOM.setStyleAttribute(mWindow, "height", Integer.toString(windowHeight));
		DOM.setStyleAttribute(mDistance, "marginBottom", Integer.toString(-(windowHeight/2)));
		
		DOM.setStyleAttribute(mWindow, "width", Integer.toString(windowWidth));
	}
	
	private class WindowResizeHandler implements ResizeHandler {
		@Override
		public void onResize(ResizeEvent event) {
			update();
		}
	}
}
